/*
 * CollaborativeWorksSubjectDao.java
 *
 * Created on November 9, 2008, 7:33 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.CollaborativeWorksSubject;

/**
 *
 * @author JollkY
 */
public class CollaborativeWorksSubjectDao implements Crud {

    private int state;
    private CollaborativeWorksSubject cws;
    private List collaborativeWorksSubjects;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();

    /** Creates a new instance of CollaborativeWorksSubjectDao */
    public CollaborativeWorksSubjectDao() {
    }

    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    public List<Object> getList() {
        this.collaborativeWorksSubjects = new ArrayList();
        try {
            em.getTransaction().begin();
            this.collaborativeWorksSubjects = em.createQuery("SELECT cws FROM CollaborativeWorksSubject cws").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.collaborativeWorksSubjects;
    }

    public Object read(long id) {
        this.cws = new CollaborativeWorksSubject();
        try {
            em.getTransaction().begin();
            this.cws = em.find(CollaborativeWorksSubject.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.cws;
    }

    public int update(Object obj) {
        this.cws = new CollaborativeWorksSubject();
        try {
            CollaborativeWorksSubject objCws = (CollaborativeWorksSubject) obj;
            em.getTransaction().begin();
            cws = em.find(CollaborativeWorksSubject.class, objCws.getId());
            cws.setTitle(objCws.getTitle());
            cws.setDescription(objCws.getDescription());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    public int delete(long id) {
        this.cws = new CollaborativeWorksSubject();
        try {
            em.getTransaction().begin();
            cws = em.find(CollaborativeWorksSubject.class, id);
            em.remove(cws);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }
}
